package com.itfreer.power.service.power.strategy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.itfreer.power.service.role.RoleDataCache;

/**
 * 角色数据缓存 缓存注册默认角色ID，默认角色ID，应用特有角色ID
 */
@Component("roleDataCacheImp")
public class RoleDataCacheImp implements RoleDataCache {

	private List<String> userDefulatRoles = new ArrayList<String>();
	private List<String> userRegisterDefulatRoles = new ArrayList<String>();
	private Map<String, List<String>> registerServiceRoles = new HashMap<String, List<String>>();

	/**
	 * 清除缓存
	 */
	public void clear() {
		userDefulatRoles.clear();
		userRegisterDefulatRoles.clear();
		registerServiceRoles.clear();
	}

	/**
	 * 添加数据
	 * 
	 * @param roleID
	 * @param isUserRegisterDefulatRole
	 * @param isUserDefulatRole
	 * @param registerService
	 */
	public void add(String roleID, Object isUserRegisterDefulatRole, Object isUserDefulatRole, Object registerService) {
		if (isUserRegisterDefulatRole != null && isUserRegisterDefulatRole.equals(true)) {
			if (!userRegisterDefulatRoles.contains(roleID)) {
				userRegisterDefulatRoles.add(roleID);
			}
		}

		if (isUserDefulatRole != null && isUserDefulatRole.equals(true)) {
			if (!userDefulatRoles.contains(roleID)) {
				userDefulatRoles.add(roleID);
			}
		}

		if (registerService != null && registerService.equals("")) {
			List<String> list = null;
			if (registerServiceRoles.containsKey(registerService)) {
				list = registerServiceRoles.get(registerService);
			} else {
				list = new ArrayList<String>();
				registerServiceRoles.put(registerService.toString(), list);
			}

			if (!list.contains(roleID)) {
				list.add(roleID);
			}
		}
	}

	/**
	 * 获取所有用户默认角色
	 * 
	 * @return
	 */
	public List<String> getUserDefulatRoles() {
		return userDefulatRoles;
	}

	/**
	 * 获取所有用户注册默认角色
	 * 
	 * @return
	 */
	public List<String> getUserRegisterDefulatRoles() {
		return userRegisterDefulatRoles;
	}

	/**
	 * 获取应用服务专要服务角色
	 * 
	 * @return
	 */
	public List<String> getRegisterServiceRoles(String registerService) {
		if (registerServiceRoles.containsKey(registerService)) {
			return registerServiceRoles.get(registerService);
		} else {
			return null;
		}
	}

}
